package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func BasicGetData(c *gin.Context) {
	name := c.Query("name") //获取URL中 ?后面的参数
	//返回字符串类型数据
	//c.String(http.StatusOK,"欢迎您：%s",name)
	//返回json数据
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK, //状态
		"msg":  "成功",          //描述信息
		"data": "欢迎您：" + name, //数据
	})
}

func BasicPostData(c *gin.Context) {
	name := c.DefaultPostForm("name", "Gin") //获取body中的数据（form形式）
	fmt.Println("name", name)
	form, err := c.GetPostForm("name") //获取body中的数据（form形式）
	fmt.Println(form, err)
	c.JSON(http.StatusOK, "欢迎您："+name)
}

func BasicCurlData(c *gin.Context) {
	url := "https://gitee.com/static/images/logo-black.svg?t=158106664"
	response, err := http.Get(url)
	if err != nil || response.StatusCode != http.StatusOK {
		c.Status(http.StatusServiceUnavailable)
		return
	}
	body := response.Body
	contentLength := response.ContentLength
	contentType := response.Header.Get("Content-Type")
	//数据写入响应体
	c.DataFromReader(http.StatusOK, contentLength, contentType, body, nil)
}
